export const DataTypes = {
  String: '[object String]',
  Array: '[object Array]',
  Object: '[object Object]',
  Number: '[object Number]',
};
HTMLElement.prototype.addStyle = function(obj:object){
  Object.keys(obj).forEach(key=>{
    this.style[key] = obj[key];
  })
};
export const isNone = data=>{return data === ''||data === undefined||data === null;};
export const isObject = data => Object.prototype.toString.call(data) === "[object Object]";
export const isArray = data => Object.prototype.toString.call(data) === "[object Array]";
export const isNumber = data => Object.prototype.toString.call(data) === "[object Number]";
export const isString = data => Object.prototype.toString.call(data) === "[object String]";

export const createCanvas = function(content:HTMLElement):HTMLCanvasElement{
  let height:number = content.clientHeight,
      width:number = content.clientWidth;
  let canvasDom:HTMLCanvasElement = document.createElement('canvas');
  canvasDom.addStyle({
    width: width+'px',
    height: height+'px'
  });
  canvasDom.setAttribute('height',height+'');
  canvasDom.setAttribute('width',width+'');
  (canvasDom as any).ctx = canvasDom.getContext('2d');
  content.appendChild(canvasDom);
  return canvasDom;
}
export const getStance = function(p1:{x:number,y:number},p2:{x:number,y:number}){
  let s_x = Math.abs(p1.x - p2.x),
      s_y = Math.abs(p1.y - p2.y);
  return ~~Math.sqrt(s_x*s_x+s_y*s_y);
}
//先计算两个圆的割线，再代入圆求交点坐标(！！！必须相交才能调用此方法)
export const getCircleIntersection = function(circle1:{x:number,y:number,radius:number},circle2:{x:number,y:number,radius:number}):Array<{x:number,y:number}>{
  let echo:Array<{x:number,y:number}> = [];
  let {x:x1,y:y1,radius:r1} = circle1,
      {x:x2,y:y2,radius:r2} = circle2;
  //为了好计算公式(因为只需割线与一个圆的焦点就可以了)
  let a = x1,b = y1,r = r1;
  //直线方程  y = kx+c
  if(y1-y2 != 0){
    let k = (x2-x1)/(y1-y2),
      c = (x1*x1-x2*x2 + y1*y1-y2*y2 + r2*r2-r1*r1)/(2*(y1-y2));
    //根据割线方程，计算交点坐标
    let $W = Math.sqrt((r*r-a*a-(c-b)*(c-b))*(1+k*k) + (c*k-a-b*k)*(c*k-a-b*k));
    let xx1 = ($W-c*k+a+b*k)/(1+k*k),
        yy1 = xx1*k+c;
    let xx2 = (-$W-c*k+a+b*k)/(1+k*k),
        yy2 = xx2*k+c;
    echo = [
      {
        x:~~xx1,
        y:~~yy1
      },
      {
        x:~~xx2,
        y:~~yy2
      }
    ];
  }else{
    let $xx = (r2*r2-r1*r1+x1*x1-x2*x2)/(2*(x1-x2));
    let $W = Math.sqrt(r*r - ($xx-a)*($xx-a));
    echo = [
      {
        x:~~$xx,
        y:~~(b+$W)
      },
      {
        x:$xx,
        y:~~(b-$W)
      }
    ];
  }
  return echo;
}
//计算两点与X轴夹角（顺时针，从x正轴开始）p1起始点，p2终点 !!!(且p1和p2不是同一个点)
export const getIntersectionAngle = function(p1:{x:number,y:number},p2:{x:number,y:number}):number{
  let echo = Math.atan((p2.y-p1.y)/(p2.x-p1.x));
  let $x = p2.x-p1.x,$y = p2.y-p1.y;
  if($x > 0 && $y > 0){
    echo = echo;
  }else if($x==0 && $y > 0){
    echo = Math.PI/2;
  }else if($x==0 && $y < 0){
    echo = Math.PI*3/2;
  }else if($x < 0 && $y > 0){
    echo = Math.PI + echo;
  }else if($x < 0 && $y == 0){
    echo = Math.PI;
  }else if($x < 0 && $y < 0){
    echo = Math.PI + echo;
  }else if($x > 0 && $y < 0){
    echo = Math.PI*2 + echo;
  }
  return echo;
}
//计算两点之间的距离和中间点坐标
export const getTwoPointMiddle = function(p1:{x:number,y:number},p2:{x:number,y:number}):{x:number,y:number,length:number} {
  let echo = {x:0,y:0,length:0};
  echo.x = ~~((p1.x+p2.x)/2);
  echo.y = ~~((p1.y+p2.y)/2);
  echo.length = ~~Math.sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y))
  return echo;
}